package it.controllocredito.backend.persistence.tables;

import it.controllocredito.backend.persistence.bean.Monitor;

import android.content.ContentValues;
import android.database.Cursor;
import android.util.Log;

public abstract class MonitorTable {

	private static final String log_class_name = "MonitorTable";

	private static final String TABLE_NAME = "monitor"; 

	public enum Columns {
		MONITOR_ID("monitorId",0),CONFIGURATION_ID("configurationId",1),MINUTI_TOT("minutiTot",2),IS_ACTIVE("isActive",3);
		
		String name; int num;
		private Columns(String name, int num){
			this.name = name; this.num = num;
		}
	};

	public static String getName(){
		return TABLE_NAME;
	}

	//metodo per ottenere la query di creazione della tablla
	public static String getCreateQuery(){
		String createQuery = "CREATE TABLE IF NOT EXISTS "
			+ MonitorTable.TABLE_NAME
			+ " (" + Columns.MONITOR_ID.toString() + " integer primary key autoincrement, "
			+ Columns.CONFIGURATION_ID.toString() + " integer not null, "
			+ Columns.MINUTI_TOT.toString() + " integer not null,"
			+ Columns.IS_ACTIVE.toString() + "integer not null;";
		Log.d(log_class_name,"createQuery:"+createQuery);
		return createQuery;
	}
	
	//crea una nuova riga sul db con tutti i campi
	public static ContentValues createRecord(int conf_id, int minuti_tot, boolean is_active) {
		
		ContentValues cv = new ContentValues();
		cv.put(Columns.CONFIGURATION_ID.toString(), conf_id);
		cv.put(Columns.MINUTI_TOT.toString(), minuti_tot);
		cv.put(Columns.IS_ACTIVE.toString(), (is_active)?0:1);
		return cv;
	}

	public static String getKeyColumn(){
		return Columns.MONITOR_ID.toString();
	}
	
	public static String[] getColumns(){
		String [] arrayTableColumns = {Columns.CONFIGURATION_ID.toString(), Columns.MINUTI_TOT.toString(), Columns.IS_ACTIVE.toString()};
		return arrayTableColumns;
	}
	
	public static Monitor getRecord(Cursor cursor){
		Monitor monitor = new Monitor();
		monitor.setMonitorId(Integer.parseInt(cursor.getString(Columns.MONITOR_ID.num)));
		monitor.setConfigurationId(Integer.parseInt(cursor.getString(Columns.CONFIGURATION_ID.num)));
		monitor.setMinutiTot(Integer.parseInt(cursor.getString(Columns.MINUTI_TOT.num)));
		monitor.setActive(((Integer.parseInt(cursor.getString(Columns.IS_ACTIVE.num)))==0)?true:false);
    	Log.d(log_class_name,"["+monitor.getMonitorId()+"] monitor: conf_id:"+monitor.getConfigurationId()+", minuti_tot:"+monitor.getMinutiTot()+", is_active:"+monitor.isActive());
    	return monitor;
	}

}